package de.tudarmstadt.gdi1.project;

import de.tudarmstadt.gdi1.project.alphabet.Alphabet;
import de.tudarmstadt.gdi1.project.alphabet.Dictionary;
import de.tudarmstadt.gdi1.project.alphabet.Distribution;
import de.tudarmstadt.gdi1.project.analysis.caeser.CaesarCryptanalysis;

/**
 * 
 * Analyses a Caesar Cipher and calculates a key. Bonus not yet implemented...
 * 
 * 
 * @author Christopher Hammel
 * 
 * @version 0.1
 *
 */

public class CaesarCipherAnalysis implements CaesarCryptanalysis {

	@Override
	public Integer knownCiphertextAttack(String ciphertext,
			Distribution distribution) {
		Alphabet alphabet = distribution.getAlphabet();
		
		Distribution distCipher = new FrequencyDistribution(alphabet, ciphertext, 1);
		
		//Beta: only using most used char, bonus ignored for now
		
		int iCipher = alphabet.getIndex(distCipher.getSorted(1).get(0).charAt(0));
		int iNorm = alphabet.getIndex(distribution.getSorted(1).get(0).charAt(0));
		
		int key = iCipher - iNorm;
		
		return key;
	}

	@Override
	public Integer knownCiphertextAttack(String ciphertext,
			Dictionary dictionary) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer knownCiphertextAttack(String ciphertext,
			Distribution distribution, Dictionary dict) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer knownPlaintextAttack(String ciphertext, String plaintext,
			Alphabet alphabet) {
		
		if(ciphertext.length() != plaintext.length() || plaintext.length() == 0){
			//Exception?
		}
		int iCipher = alphabet.getIndex(ciphertext.charAt(0));
		int iPlain = alphabet.getIndex(plaintext.charAt(0));
		
		return iCipher - iPlain;
	}

	@Override
	public Integer knownPlaintextAttack(String ciphertext, String plaintext,
			Distribution distribution) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer knownPlaintextAttack(String ciphertext, String plaintext,
			Distribution distribution, Dictionary dictionary) {
		// TODO Auto-generated method stub
		return null;
	}

}
